
const { ccclass, property, menu, requireComponent } = cc._decorator;
/** PageViewAutoScroll 自动滚动PageView
 * 
 */
@ccclass
@menu("自定义组件/PageView/自动滚动")
export default class PageViewAutoScroll extends cc.Component {
    @property(cc.PageView)
    pv_PageView: cc.PageView = null
    @property({
        type: 0,
        tooltip: "间隔时间,秒(s)"
    })
    dt_ChangeStep: Number = 0

    _CumulativeDt
    /** 开关 */
    _Switch: boolean
    onLoad() {
        this.pv_PageView || (this.pv_PageView = this.node.getComponent(cc.PageView))
        this.pv_PageView || cc.error("未正确取得数据")
    }
    onEnable() {
        this._CumulativeDt = 0;
        this._Switch = true;
        this.pv_PageView.node.on('scrolling', this._PageTurning, this)
        this.pv_PageView.node.on('scroll-ended', this._PageTurning, this)
    }
    onDisable() {
        this._Switch = false;
        this.pv_PageView.node.off('scrolling', this._PageTurning, this)
        this.pv_PageView.node.off('scroll-ended', this._PageTurning, this)
    }
    _PageTurning() {
        this._CumulativeDt = 0;
    }
    update(dt) {

        this._CumulativeDt += dt;
        if (this._Switch && this.pv_PageView && this._CumulativeDt > this.dt_ChangeStep) {
            this._CumulativeDt = 0;
            let nowPage = this.pv_PageView.getCurrentPageIndex();
            let totalPage = this.pv_PageView.getPages().length;
            let nextPage = (nowPage + 1) % totalPage

            this.pv_PageView.scrollToPage(nextPage, 0.5)
        }
    }
}
